package com.luocc.tim.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.luocc.tim.MainActivity;
import com.luocc.tim.R;
import com.luocc.tim.recycler.ChatListAdapter;
import com.luocc.tim.recycler.ChatListRecycler;
import com.luocc.tim.entity.Message;
import com.luocc.tim.header.TimHeader;
import com.luocc.tim.utils.TimConstants;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChatListFragment extends Fragment {

    private static final String TAG = "MessageFragment";

    public static final long DAY = 24 * 60 * 60 * 1000L;
    public static final long DAY_30 = 30 * 24 * 60 * 60 * 1000L;
    public static final long DAY_30_12 = 12 * 30 * 24 * 60 * 60 * 1000L;

    private MainActivity mContext;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = (MainActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMessages();
    }

    private List<Message> getMessages() {
        List<Message> messages = new ArrayList<>();
        Message message1 = new Message("文榜", "luocc", "你今天吃的啥", new Date(System.currentTimeMillis() - DAY_30_12 * 3), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message2 = new Message("智豪", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY_30_12), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message3 = new Message("强哥", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY / 10), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message4 = new Message("陈胜", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY_30), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message5 = new Message("吴广", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY_30 * 10), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message6 = new Message("老马", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY_30_12 * 10), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message7 = new Message("张艾", "luocc", "快来领红包", new Date(System.currentTimeMillis() - DAY / 3), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message8 = new Message("桂标", "luocc", "你吃了没", new Date(System.currentTimeMillis() - DAY / 6), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message9 = new Message("luocc", "老魏", "你没上班吗今天", new Date(System.currentTimeMillis() - DAY), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message10 = new Message("luocc", "老谭", "你没上班吗今天", new Date(System.currentTimeMillis() - DAY * 10), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);
        Message message11 = new Message("luocc", "老林", "你没上班吗今天", new Date(System.currentTimeMillis()), TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT);


        Message refresh = new Message(TimConstants.ItemType.ITEM_TYPE_REFRESH);
        Message search = new Message(TimConstants.ItemType.ITEM_TYPE_SEARCH);
        messages.add(refresh);
        messages.add(search);

        messages.add(message1);
        messages.add(message2);
        messages.add(message3);
        messages.add(message4);
        messages.add(message5);
        messages.add(message6);
        messages.add(message7);
        messages.add(message8);
        messages.add(message9);
        messages.add(message10);
        messages.add(message11);
        return messages;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat_list, container, false);
        setHeader(view);
        ChatListRecycler mChatListRecycler = view.findViewById(R.id.chat_list);
        List<Message> messages = getMessages();
        LinearLayoutManager mManager = new LinearLayoutManager(mContext);
        mManager.setOrientation(LinearLayoutManager.VERTICAL);
        mChatListRecycler.setLayoutManager(mManager);
        assert getParentFragment() != null;
        mChatListRecycler.setAdapter(new ChatListAdapter(mContext, messages));
        return view;
    }

    private void setHeader(View view) {
        TimHeader header = view.findViewById(R.id.header);
        header.setText(R.string.message);
        header.setImage(R.mipmap.more_normal);
        header.setImageClickListener(v -> {
            header.setImage(R.mipmap.more_open);
        });
    }
}
